public without sharing class ReceivableController{
   public String  companyJson {get; private set;}//可选择的公司;
   public List<SelectOption> companytags {get;set;}//公司对应编码;
   public String company {get;set;}//公司
   public String conCodes {get;set;}//公司的编号
   public String compk {get;set;}//公司的pk
   public List<ReceivableDetails> des {get;set;}//查询结果
   public Account acc {get;set;}//客户 
   public List<List<ReceivableDetails>> rds {get;private set;}//换页处理;
   public Boolean isAll {get;set;}//是否查询全部数据
    public Boolean isAssistant {get;set;}//是否可以查所有公司的对账意想

   public Integer pageCnt {get;private set;}//页数
   //参数
   public String enddate {set;get;}//截止日期
   
   public ReceivableController(ApexPages.StandardController con){

   } 
     public ReceivableController(){
   
         System.debug(ApexPages.currentPage().getUrl());
         des = new List<ReceivableDetails>();
         rds = new List<List<ReceivableDetails>>();
         enddate = System.currentPageReference().getParameters().get('enddate');
         String c = System.currentPageReference().getParameters().get('c');

          User u = [select Id,CrossCompany__c,SubCompany__c from User where Id =: UserInfo.getUserId()];
          isAssistant = u.CrossCompany__c != null && u.CrossCompany__c == true ; 

          company = u.SubCompany__c;
          List<Object> companys = new List<Object>();

         for (String str : System.Label.Corpcode.split(',', -1)) {
              List<String> cs = str.split('-');
              Map<String,String> companyInfo = new Map<String,String>();
             if(company ==cs[1]){

              conCodes = cs[2];
              compk = cs[0];

            }
             if(cs.size() > 2){
              companyInfo.put('id', cs[0]);
              companyInfo.put('text', cs[1]);
              companyInfo.put('pk', cs[2]);
              companys.add(companyInfo);
             }
             


          }  
          companyJson = EncodingUtil.urlEncode(JSON.serialize(companys ), 'UTF-8');
         acc = [select Id,Name,AccountNumber,Phone from Account where Id =:System.currentPageReference().getParameters().get('accid')];

         isAll = System.currentPageReference().getParameters().get('isAll') == '1' ? true :false;
 //加载对账单数据 余额不为零
        if(System.currentPageReference().getUrl().contains('ReceivablePDF')){
          System.debug('isAll'+isAll);
            if (isAll) {
                loadReceivableAll();
            }else{

                loadReceivable();
            }
            
        }
   } 
   
 
   public void init(){
              des = new List<ReceivableDetails>(); 
          /*
       companys = new List<SelectOption>();
       companytags = new List<SelectOption>();
       companys.add(new SelectOption('', System.Label.Option_None));
       for (String str : System.Label.Corpcode.split(',', -1)) {
           List<String> cs = str.split('-');
            companys.add(new SelectOption(cs[0], cs[1]));
        }
        */

       acc = [select Id,Name,AccountNumber from Account where Id =:System.currentPageReference().getParameters().get('accid')];
      

   }
  
  
 
  public void loadReceivableAll( ){

    System.debug('查询全部');

    ///请求参数处理
       Map <String,String> params = new Map<String ,String>();

      String sdate = System.currentPageReference().getParameters().get('date');
      if (sdate == null || sdate.length()==0) {
        sdate = '2012-01-01';
      }
      params.put('date',sdate);
      params.put('pk_corp',System.currentPageReference().getParameters().get('c'));
      params.put('enddate',System.currentPageReference().getParameters().get('enddate'));
      params.put('custcode',System.currentPageReference().getParameters().get('custcode'));
      //params.put('custcode','602023');
      List <Map<String,String>> d = new List<Map<String,String>>();
      d.add(params);
      Map<String,List<Map<String,String>>> data = new Map<String ,List<Map<String,String>>>();
      data.put('data',d);
      String body = JSON.serialize(data);
      //System.debug(body);
      //请求
      HttpRequest req = new HttpRequest();
      req.setBody(body);
      req.setMethod('POST');
      req.setHeader('Content-Type','text/plain');
      String url = System.Label.queryReceiveAllURL;
      req.setEndpoint(url);

            
      HttpResponse res = Utils.postRequest(req, 3);
      setData(res);

  }

  public void setData(HttpResponse res ){
      ///返回结果处理
        if (res.getStatusCode() == 200) {

               Map<String,Object> r =null;
                 //如果是测试
                if(Test.isRunningTest()){
                  Map<String,Object>  resBody= new Map<String,Object>();
                  resBody.put('code','0');
                  resBody.put('result','{\"result_msg\":\"查询成功！\",\"result\":1,\"data\":[{\"corpcode\":\"111\",\"custname\":\"111\",\"deptcode\":\"111\",\"deptname\":\"111\",\"jfbbje\":\"111\",\"dfbbje\":\"111\",\"bbye\":\"111\",\"xydqr\":\"111\",\"ye\":\"111\",\"psncode\":\"111\",\"psnname\":\"111\",\"prodlinecode\":\"111\",\"prodlinename\":\"111\",\"djrq\":\"111\",\"djbh\":\"111\",\"currtypename\":\"111\"}]}');
                    r =  resBody;
                }else{
                  r= (Map<String,Object> ) JSON.deserializeUntyped(res.getBody());
                }
        System.debug(res.getBody());
          if(r.get('code') == 0|| r.get('code')=='0' ){
               Map<String,Object> rs = (Map<String,Object>) JSON.deserializeUntyped(((String)r.get('result')));
            if( rs.get('result') == 1 ){
                List<Object> datas = (List<Object>)rs.get('data');
                
                for(Object ob :datas){
                ReceivableDetails rd = new ReceivableDetails((Map<String,Object>)ob);
                    des.add(rd);
                }
                
                List<ReceivableDetails> temp = new List<ReceivableDetails>();
                Boolean flag = true;
                Decimal h = 0;
                for(Integer i = 0;i < des.size() ; i ++){
                       ReceivableDetails d1 = des[i];
                       temp.add(d1);
                       if((rds.size() == 0 && temp.size() == 31) || temp.size() == 33){
                        List<ReceivableDetails> t = new List<ReceivableDetails>();
                        t.addAll(temp);
                        rds.add(t);
                        temp.clear();
                       }  
                }
               
                if(temp.size() >0)rds.add(temp);
                pageCnt = rds.size();
                
              }
                          
          }
    
      }
  }
  public void loadReceivable (){
    System.debug('查询不为零');
  
      HttpRequest req = new HttpRequest();
      //req.setBody(body);
      req.setMethod('POST');
      req.setHeader('Content-Type','text/plain');
      String url = System.Label.queryReceiveURL +'&custcode='+System.currentPageReference().getParameters().get('custcode');
      url = url + '&enddate='+System.currentPageReference().getParameters().get('date');
      url = url + '&company=' +System.currentPageReference().getParameters().get('c');
      req.setEndpoint(url);
            
      HttpResponse res = Utils.postRequest(req, 3);
      setData(res);
      /*
        if (res.getStatusCode() == 200) {

                 Map<String,Object> r = null;

                 //如果是测试
                if(Test.isRunningTest()){
                  Map<String,Object>  resBody= new Map<String,Object>();
                  resBody.put('code','0');
                  resBody.put('result','{\"result_msg\":\"查询成功！\",\"result\":1,\"data\":[{\"corpcode\":\"111\",\"custname\":\"111\",\"deptcode\":\"111\",\"deptname\":\"111\",\"jfbbje\":\"111\",\"dfbbje\":\"111\",\"bbye\":\"111\",\"xydqr\":\"111\",\"ye\":\"111\",\"psncode\":\"111\",\"psnname\":\"111\",\"prodlinecode\":\"111\",\"prodlinename\":\"111\",\"djrq\":\"111\",\"djbh\":\"111\",\"currtypename\":\"111\"}]}');
                    r =  resBody;
                }else{

                  r= (Map<String,Object> ) JSON.deserializeUntyped(res.getBody());
                }
          if(r.get('code') == 0 || r.get('code')=='0'){
               Map<String,Object> rs = (Map<String,Object>) JSON.deserializeUntyped(((String)r.get('result')));
            if( rs.get('result') == 1 ){
                List<Object> datas = (List<Object>)rs.get('data');
                
                for(Object ob :datas){
                ReceivableDetails rd = new ReceivableDetails((Map<String,Object>)ob);
                    des.add(rd);
                }
                List<ReceivableDetails> temp = new List<ReceivableDetails>();
                Boolean flag = true;
                Decimal h = 0;
                for(Integer i = 0;i < des.size() ; i ++){
                       ReceivableDetails d1 = des[i];
                       temp.add(d1);
                       if((rds.size() == 0 && temp.size() == 31) || temp.size() == 33){
                        List<ReceivableDetails> t = new List<ReceivableDetails>();
                        t.addAll(temp);
                        rds.add(t);
                        temp.clear();

                       }

                  
                }

                if(temp.size() >0)rds.add(temp);
                pageCnt = rds.size();
              }
                          
          }
    
      }*/
  }
   public class ReceivableDetails {

 
      public  String corpcode {set;get;}
      public  String dbilldate {set;get;}//单据日期
      public  String dbillcode {set;get;}//发票号
      public  String zy {set;get;}//摘要
       
      public  String custcode {set;get;}
      public  String custname {set;get;}
      public  String deptcode {set;get;}
      public  String deptname {set;get;}
       
      public  String jfbbje {set;get;}//本期应收
      public  String dfbbje {set;get;}//本期收回
      public  String bbye {set;get;}//单据余额
      public  String xydqr {set;get;}//到期日
       
      public  String ye {set;get;}//余额
      public  String fph {set;get;}
      public  String km {set;get;}
      public  String subjname {set;get;}
       
      public  String psncode {set;get;}
      public  String psnname {set;get;}
      public  String prodlinecode {set;get;}
      public  String prodlinename {set;get;}
      public  String djrq {get;set;}
      public  String djbh{get;set;}

      public String currtypename {get;set;}
      
      public ReceivableDetails(){}
      public ReceivableDetails(Map<String,Object> r){

               
          corpcode = String.valueOf(r.get('corpcode'));
          dbilldate =String.valueOf(r.get('dbilldate'));
          dbillcode = String.valueOf(r.get('dbillcode'));
          zy = String.valueOf(r.get('zy'));

          custcode = String.valueOf(r.get('custcode'));
          custname = String.valueOf(r.get('custname'));
          deptcode = String.valueOf(r.get('deptcode'));
          deptname = String.valueOf(r.get('deptname'));

          jfbbje = String.valueOf(r.get('jfbbje') == null ? '' :r.get('jfbbje'));
          dfbbje = String.valueOf(r.get('dfbbje') == null ? '' :r.get('dfbbje'));
          bbye = String.valueOf(r.get('bbye') == null ? '': r.get('bbye'));
          xydqr = String.valueOf(r.get('xydqr'));

          ye = String.valueOf(r.get('ye') == null ? '' :r.get('ye'));
          fph = String.valueOf(r.get('fph'));
          km = String.valueOf(r.get('km'));
          subjname = String.valueOf(r.get('subjname'));

          psncode = String.valueOf(r.get('psncode'));
          psnname = String.valueOf(r.get('psnname'));
          prodlinecode = String.valueOf(r.get('prodlinecode'));
          prodlinename = String.valueOf(r.get('prodlinename'));
          
          djrq = String.valueOf(r.get('djrq'));
          djbh = String.valueOf(r.get('djbh'));

          currtypename = String.valueOf(r.get('currtypename'));
      }

   } 
}